//index.js
//获取应用实例
var app = getApp()
Page({
    data: {
        hiddenLoading: false,
        data: null,
        title: null,
        imgurl: app.urlimg,
        defaultpages: "2",   //显示全部帖子的页码
        loadHidden: true,
        price_order: 0,
        loadText: "加载中...",
        toastHidden: true,
        msg: '没有更多商品了',
        key: '',
        category_id: 0,
        new_content: [],
        imgurl: app.urlimg,
        // 条件切换
        conditions: 'null',
        // 价格选择框高度
        price_height: '-6',
        contraction: true,
        //用户输入的最小值
        min_interval: '',
        //用户输入的最大值
        max_interval: '',
        defaultpages: 2,
        switchs: '',
        toTop: '',
        loading_ing: true,
    },
    switchs: function (e) {
        var that = this
        that.setData({ loadHidden: false, toTop: 0, })
        wx.request({
            url: app.url + 'api_goods/goods_list',
            data: {
                'switchs': e.currentTarget.dataset.id,
                name: that.data.key,
                category_id: that.data.category_id,
            },
            header: {
                'content-type': 'application/json',
                'token': wx.getStorageSync('token_key')
            }, success: function (json) {
                that.setData({
                    data: json.data.result,
                    conditions: e.currentTarget.dataset.id,
                    min_interval: null,
                    defaultpages: 2,
                    price_height: -6,
                    price_order: 0,
                    loadHidden: true,
                })
                var price_height = parseInt(that.data.price_height);
            }
        })


    },
    onLoad: function (o) {
        var that = this
        if (typeof (o.footer_cat_ids) == 'undefined') {
            o.footer_cat_ids = 0
        }

        if (typeof (o.category_id) == 'undefined') {
            o.category_id = 0
        }

        if (typeof (o.key) == 'undefined') {
            o.key = ''
        } else {
            wx.setNavigationBarTitle({
                title: o.key,
            })
        }
        
        var search_bar = true;
        if (typeof (o.name) != 'undefined') {
            search_bar = false;
            wx.setNavigationBarTitle({
                title: o.name,
            })
        }
        //判断搜索框是否显示
        that.setData({
          search_bar: search_bar,
        })



        that.list(decodeURI(o.key), o.category_id, o.footer_cat_ids)
    },
    search: function (e) {
        var that = this
        var value = e.detail.value.input;
        if (value == '') {
            wx.showModal({
                title: '提示',
                content: '请输入关键词'
            })
        } else {
            that.setData({
                title: value,
            })

            wx.request({
                url: app.url + 'api_goods/goods_list',
                data: {
                    name: value,
                    category_id: that.data.category_id
                },
                header: {
                    'content-type': 'application/json',
                    'token': wx.getStorageSync('token_key')
                }, success: function (json) {

                    that.setData({
                        data: json.data.result,
                        key: value,
                        toTop:0,
                    })


                }
            })
        }
    },
    scroll: function (event) {
        this.setData({
            "toTop": event.detail.scrollTop
        });
        // console.log(this.data.toTop);
    },
    // 获取限制的价格区间
    interval: function (e) {
        var that = this
        //最大值
        console.log(e.currentTarget.dataset.max);
        //最小值
        console.log(e.currentTarget.dataset.min);
        var price = ''
        if (e.currentTarget.dataset.min == 500) {
            price = '500'
            e.currentTarget.dataset.max = ''
        } else {
            price = e.currentTarget.dataset.min + ',' + e.currentTarget.dataset.max
        }
        wx.request({
            url: app.url + 'api_goods/goods_list',
            data: {
                'price': price,
                name: that.data.key,
                category_id: that.data.category_id,
            },
            header: {
                'content-type': 'application/json',
                'token': wx.getStorageSync('token_key')
            }, success: function (json) {
                that.setData({
                    data: json.data.result,
                    min_interval: e.currentTarget.dataset.min,
                    max_interval: e.currentTarget.dataset.max,


                })
            }
        })

        // 获取当前位置
        var that = this;
        var price_height = parseInt(that.data.price_height);
        // 上升
        var time = setInterval(function () {
            if (price_height > -6) {
                price_height -= 1;
                that.setData({
                    price_height: price_height,
                })
            } else {
                clearInterval(time);
                time = null;
            }
        }, 15)
    },
    /*获取用户输入的最小值 */
    user_min_interval: function (e) {
        this.setData({
            min_interval: e.detail.value,
        })
    },
    /*获取用户输入的最大值 */
    user_max_interval: function (e) {
        console.log(e.detail.value);
        this.setData({
            max_interval: e.detail.value,
        })
    },
    // 确定按钮
    ko_btn: function (e) {
        var that = this;
        //数据加载
        var min_price = e.detail.value.user_min_interval
        var max_price = e.detail.value.user_max_interval
        if (min_price == '' || max_price == '') {
            wx.showModal({
                title: '提示',
                content: '请输入价格区间',
            })
        }
        var price = min_price + ',' + max_price
        wx.request({
            url: app.url + 'api_goods/goods_list',
            data: {
                'price': price,
                name: that.data.key,
                category_id: that.data.category_id,
            },
            header: {
                'content-type': 'application/json',
                'token': wx.getStorageSync('token_key')
            }, success: function (json) {
                that.setData({
                    data: json.data.result, min_interval: min_price,
                    max_interval: max_price
                })
            }
        })
        var price_height = parseInt(that.data.price_height);
        // 上升
        var time = setInterval(function () {
            if (price_height > -6) {
                price_height -= 1;
                that.setData({
                    price_height: price_height,
                })
            } else {
                clearInterval(time);
                time = null;
            }
        }, 15)
    },

    goodslink: function (e) {
        var fid = e.currentTarget.id
        var regimentId = e.currentTarget.dataset.regiment_id;
        var groupType = e.currentTarget.dataset.group_type;
        var is_expire = e.currentTarget.dataset.is_expire;
        if (regimentId != null && groupType != null) {
            // { id: "2829", regiment_id: "76", group_type: "3" }
            //console.log('/pages/group/groupdetail/index?id=' + fid + "&regiment_id=" + regimentId + "&group_type=" + groupType)
            if(groupType==1 && is_expire ==3){
              wx.navigateTo({
                url: '/pages/index/detail/index?id=' + fid,
              })
            }else{
              wx.navigateTo({
                url: '/pages/group/groupdetail/index?id=' + fid + "&regiment_id=" + regimentId + "&group_type=" + groupType,
              })
            }
          
        } else {
            wx.navigateTo({
                url: '/pages/index/detail/index?id=' + fid,
            })
        }
    },
    // 价格选择
    price_select: function () {
        var that = this;
        // 判断上下拉
        that.setData({ loadHidden: false, toTop: 0, })
        var price_order = that.data.price_order == 1 ? 2 : 1
        /* that.setData({price_order:price_order,conditions:3})*/
        /*contraction: !contraction,*/
        var contraction = that.data.contraction;

        wx.request({
            url: app.url + 'api_goods/goods_list',
            data: {
                'switchs': 4,
                name: that.data.key,
                category_id: that.data.category_id,
                price_order: price_order
            },
            header: {
                'content-type': 'application/json',
                'token': wx.getStorageSync('token_key')
            }, success: function (json) {
                that.setData({
                    data: json.data.result,
                    defaultpages: 2,
                    price_order: price_order,
                    conditions: 4,
                    defaultpages: 2,
                    loadHidden: true,
                })
                var price_height = parseInt(that.data.price_height);
            }


        })












        /*that.setData({
         contraction: !contraction,
       })*/
        // 获取当前位置
        var price_height = parseInt(that.data.price_height);
        //下拉
        function add() {
            if (price_height < 7) {
                price_height += 1;
                that.setData({
                    price_height: price_height,
                })
            } else {
                clearInterval(time);
                time = null;
            }
        }
        // 上升
        function subtract() {
            if (price_height > -6) {
                price_height -= 1;
                that.setData({
                    price_height: price_height,
                })
            } else {
                clearInterval(time);
                time = null;
            }
        }
        var time = setInterval(function () {
            if (that.data.contraction == false) {
                add()
            }
            if (that.data.contraction == true) {
                subtract()
            }
        }, 15)


    },


    list: function (value, category, footer_cat_ids) {
        var that = this
        wx.request({
            url: app.url + 'api_goods/goods_list',
            data: {
                name: value,
                category_id: category,
                footer_cat_ids: footer_cat_ids
            },
            header: {
                'content-type': 'application/json',
                'token': wx.getStorageSync('token_key')
            }, success: function (json) {
                console.log(json.data.result)
                that.setData({
                    category_id: category,
                    footer_cat_ids: footer_cat_ids,
                    key: value,
                    data: json.data.result,
                    hiddenLoading: !that.data.hiddenLoading
                })

            }
        })

    },


    //下拉加载下一页
    // onReachBottom: 
    loadMore: function () {
        var page = this;
        console.log(page.data.toTop);
        if (page.data.loading_ing) {
            page.setData({ loadHidden: false, price_height: -6, loading_ing: false, })
            console.log('----------------------------------')
            var price = page.data.min_interval + ',' + page.data.max_interval
            if (page.data.min_interval == '' || page.data.min_interval == null) {
                price = ''
            }
            if (page.data.conditions == null || page.data.conditions == 'null') {
                page.data.conditions = ''
            }
            var forumPage = page.data.defaultpages;
            wx.request({
                url: app.url + 'api_goods/goods_list',
                data: {
                    name: page.data.key,
                    page: page.data.defaultpages,
                    category_id: page.data.category_id,
                    footer_cat_ids: page.data.footer_cat_ids,
                    switchs: page.data.conditions,
                    price: price,
                    price_order: page.data.price_order,
                },
                header: {
                    'content-type': 'application/json',
                    'token': wx.getStorageSync('token_key')
                },
                success: function (res) {
                    if (res.data.result.length > 0) {
                        var goods = page.data.data;
                        var newForum = goods.concat(res.data.result);
                        page.setData({ defaultpages: ++page.data.defaultpages, data: newForum, loading_ing: true, });
                        //page.setData({show_loading:'none'});
                    } else {
                        // page.setData({show_loading:'none'});
                        // page.setData({ loadText: "最后一页了" })
                        page.setData({ toastHidden: false })
                    }

                }, complete: function () {
                    //显示出加载中的提示
                    page.setData({ loadHidden: true })
                }
            })
        }
    },



    // 响应分类单击
    goodTAP: function (e) {
        var fid = e.currentTarget.id
        wx.navigateTo({
            url: '/pages/index/detail/index?id=' + fid
        })
    },
    go_home: function () {
        wx.redirectTo({
            url: '/pages/index/index',
        })
    },

    //分享
    onShareAppMessage: function () {

    },


})
